package com.jeesite.modules.logistics.web.updatecarframenumber;

import com.alibaba.fastjson.JSONArray;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.logistics.service.updatecarframenumber.VehicleLogisticsStatusQueryService;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrderDetail;
import com.jeesite.modules.order.service.serviceorder.ServiceOrderService;
import com.jeesite.modules.sys.utils.DictUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 车辆物流状态查询更新Controller
 *
 * @author Kilo
 * @version 2021-03-05
 */
@Controller
@RequestMapping(value = "${adminPath}/logistics/vehicleLogisticsStatusQuery")
public class VehicleLogisticsStatusQueryController extends BaseController {

    @Autowired
    private VehicleLogisticsStatusQueryService vehicleLogisticsStatusQueryService;
    @Autowired
    private ServiceOrderService serviceOrderService;

    /**
     * 查询列表
     */
    @RequiresPermissions("logistics:vehicleLogisticsStatusQuery:view")
    @RequestMapping(value = {"updaleList", ""})
    public String updaleList(ServiceOrder serviceOrder, Model model) {
        model.addAttribute("serviceOrder", serviceOrder);
        return "modules/logistics/updatecarframenumber/vehicleLogisticsStatusQuery";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("logistics:vehicleLogisticsStatusQuery:view")
    @RequestMapping(value = "updateNumberListData")
    @ResponseBody
    public Page < ServiceOrder > updateNumberListData(ServiceOrder serviceOrder, HttpServletRequest request, HttpServletResponse response) {

        serviceOrder.setPage(new Page <>(request, response));
        Page < ServiceOrder > page = vehicleLogisticsStatusQueryService.updateNumber(serviceOrder);
        return page;

    }

    /**
     * 保存数据
     */
    @RequiresPermissions("logistics:vehicleLogisticsStatusQuery:edit")
    @PostMapping(value = "updateStatus")
    @ResponseBody
    @Transactional(readOnly = false)
    public String updateFrameNumber(@RequestParam("NumberArr") String jsonStr) {
        List < VehicleLogisticsStatusQueryController.Obj > objList = JSONArray.parseArray(jsonStr, VehicleLogisticsStatusQueryController.Obj.class);
        ServiceOrder serviceOrder = null;
        ServiceOrderDetail serviceOrderDetail = null;
        for (VehicleLogisticsStatusQueryController.Obj obj : objList) {
            serviceOrder = new ServiceOrder();
            serviceOrder.setServiceOrderCode(obj.getServiceOrderCode());
            serviceOrder = serviceOrderService.findList(serviceOrder).get(0);
            serviceOrderDetail = new ServiceOrderDetail();
            serviceOrderDetail.setOrderId(serviceOrder.getServiceOrderId());
            serviceOrderDetail.setFrameNumber(obj.getFrameNumber());
            serviceOrderDetail.setLogisticsStatus(DictUtils.getDictValue("erp_logistics_status", obj.getStatus(), ""));

            vehicleLogisticsStatusQueryService.updateDetail(serviceOrderDetail);
        }
        return renderResult(Global.TRUE, text("修改成功！"));
    }


    public static class Obj {
        private String serviceOrderCode;
        private String frameNumber;
        private String Status;

        public String getServiceOrderCode() {
            return serviceOrderCode;
        }

        public void setServiceOrderCode(String serviceOrderCode) {
            this.serviceOrderCode = serviceOrderCode;
        }

        public String getFrameNumber() {
            return frameNumber;
        }

        public void setFrameNumber(String frameNumber) {
            this.frameNumber = frameNumber;
        }

        public String getStatus() {
            return Status;
        }

        public void setStatus(String status) {
            Status = status;
        }
    }
}
